package com.investindo.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;
import net.sf.json.JSONSerializer;

import com.google.gson.Gson;
import com.investindo.bean.Ativo;
import com.investindo.bean.Usuario;
import com.investindo.monitoramento.MonitorarBolsa;
import com.investindo.monitoramento.test;
import com.investindo.util.AtivosUtil;
import com.investindo.util.UsuarioUtil;

public class InvestindoServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private String storeId;

	private int idUsuario = 0;

	public InvestindoServlet() {
		super();
	}

	@Override
	public void init() throws ServletException {
		new MonitorarBolsa().start();
	}

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		int action = Integer.parseInt(request.getParameter("store_id"));

		AtivosUtil informacoesAtivos = new AtivosUtil();
		switch (action) {
		case 1:
			this.todosAtivos(response);
			break;
		case 7:
			this.getDetalheAtivo("", response);
			break;

		default:
			break;
		}

	}

	protected void doPut(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		int action = Integer.parseInt(request.getParameter("actionId"));
		String email = request.getParameter("email");
		String senha = request.getParameter("senha");
		switch (action) {
		case 5:
			logar(email, response);
			break;

		default:
			break;
		}

		System.out.println("do putttt");
	}

	private void getDetalheAtivo(String ativoCod, HttpServletResponse response) {
		AtivosUtil informacoesAtivos = new AtivosUtil();

		JSONObject meuObj = informacoesAtivos.getDetalheAtivo(ativoCod);

		response.setContentType("text/html");
		// PrintWriter print;
		PrintWriter out;
		try {
			out = response.getWriter();

			response.setContentType("text/html");

			out.println(meuObj.toString());
			out.close();

		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	private void todosAtivos(HttpServletResponse response) {
		AtivosUtil informacoesAtivos = new AtivosUtil();

		String text = informacoesAtivos.getTodosAtivos();

		response.setContentType("text/html");
		PrintWriter print;
		try {
			print = response.getWriter();
			print.print(text);
			print.flush();
			print.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		storeId = request.getParameter("store_id");
		int strId = Integer.parseInt(request.getParameter("store_id"));
		String info = request.getParameter("recordInfo");
		String valor1 = request.getParameter("valor1");

		switch (strId) {
		case 1:

			break;
		case 2:

			break;
		case 3:
			this.getDetalheAtivo(info.trim(), response);
			break;
		case 4:
			this.logar(valor1, response);
			break;
		case 5:
			this.salvarUsuario(valor1, response);
			break;
		default:
			break;
		}
		// get the request type update or insert
		String action = request.getParameter("action");

		// if update
		if (action.trim().equalsIgnoreCase("update")) {
			// updateAtivo(request, response);
		}
		// if insert
		else if (action.trim().equalsIgnoreCase("insert")) {
		}

	}

	private void salvarUsuario(String valor1, HttpServletResponse response) {
		UsuarioUtil usUtil = new UsuarioUtil();
		Gson gson = new Gson();
		Usuario usuario = new Usuario();
		usuario = gson.fromJson(valor1, Usuario.class);

		String text = usUtil.novoUsuario(usuario);

		idUsuario = usuario.getId();
		response.setContentType("text/html");
		PrintWriter print;
		try {
			print = response.getWriter();
			print.print(text);
			print.flush();
			print.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	private void logar(String valor1, HttpServletResponse response) {
		UsuarioUtil usUtil = new UsuarioUtil();
		Usuario usuario = new Usuario();
		Gson gson = new Gson();
		usuario = gson.fromJson(valor1, Usuario.class);
		int idUsuario = usUtil.login(usuario);

		PrintWriter out;
		try {
			out = response.getWriter();

			response.setContentType("text/html");

			// send response back whether the request was successful
			JSONObject myObj = new JSONObject();
			if (idUsuario > -1) {
				myObj.put("success", true);
				myObj.put("usuarioId", idUsuario);
			} else {
				myObj.put("success", false);
			}
			out.println(myObj.toString());
			out.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		// try {
		// print = response.getWriter();
		// print.print(text);
		// print.flush();
		// print.close();
		// } catch (IOException e) {
		// // TODO Auto-generated catch block
		// e.printStackTrace();
		// }

	}
}